package blockchain

import (
	"context"
	"private-domain-overseas-service/app/business/api/internal/svc"
	"private-domain-overseas-service/pkg/common"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzblockchainwalletrecord"
	"private-domain-overseas-service/tools/ent/predicate"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type BlockchainWalletRecordDao struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewBlockchainWalletRecordDao(ctx context.Context, svcCtx *svc.ServiceContext) *BlockchainWalletRecordDao {
	return &BlockchainWalletRecordDao{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// BlockchainWalletRecordFirst 根据条件查询单个区块链钱包记录
func (l *BlockchainWalletRecordDao) BlockchainWalletRecordFirst(conditions []predicate.HzBlockchainWalletRecord) (*ent.HzBlockchainWalletRecord, error) {
	record, err := l.svcCtx.Orm.HzBlockchainWalletRecord.Query().
		Where(conditions...).
		First(l.ctx)
	if err != nil {
		return nil, err
	}
	return record, nil
}

// BlockchainWalletRecordConditionsPageAll - 分页条件获取所有区块链钱包记录
func (l *BlockchainWalletRecordDao) BlockchainWalletRecordConditionsPageAll(conditions []predicate.HzBlockchainWalletRecord, current, pageSize int64) ([]*ent.HzBlockchainWalletRecord, int, error) {

	// 计算偏移量
	offset := common.Offset(current, pageSize)

	// 查询记录列表
	records, err := l.svcCtx.Orm.HzBlockchainWalletRecord.Query().
		Where(conditions...).
		Order(ent.Desc(hzblockchainwalletrecord.FieldCreatedAt)).
		Offset(int(offset)).
		Limit(int(pageSize)).
		All(l.ctx)
	if err != nil {
		return nil, 0, err
	}
	// 查询总数
	count, err := l.svcCtx.Orm.HzBlockchainWalletRecord.Query().
		Where(conditions...).
		Count(l.ctx)
	if err != nil {
		return nil, 0, err
	}
	return records, count, nil
}

// CreateBlockchainWalletRecord 创建区块链钱包记录
func (l *BlockchainWalletRecordDao) CreateBlockchainWalletRecord(tx *ent.Tx, record *ent.HzBlockchainWalletRecord) (*ent.HzBlockchainWalletRecord, error) {
	clientDao := l.svcCtx.Orm.HzBlockchainWalletRecord
	if tx != nil {
		clientDao = tx.HzBlockchainWalletRecord
	}
	// 执行添加
	result, err := clientDao.Create().
		SetUserID(record.UserID).
		SetBlockchainWalletID(record.BlockchainWalletID).
		SetRecordNumber(record.RecordNumber).
		SetRecordIntegralAmount(record.RecordIntegralAmount).
		SetRecordHzbCoin(record.RecordHzbCoin).
		SetRecordRates(record.RecordRates).
		SetRecordServiceCharge(record.RecordServiceCharge).
		SetRecordTitle(record.RecordTitle).
		SetRecordStatus(record.RecordStatus).
		SetRecordWalletAddress(record.RecordWalletAddress).
		SetCreatedAt(time.Now().Unix()).
		SetUpdateAt(time.Now().Unix()).
		Save(l.ctx)
	if err != nil {
		return nil, err
	}
	return result, nil
}

// UpdateBlockchainWalletRecord - 更新区块链钱包记录
func (l *BlockchainWalletRecordDao) UpdateBlockchainWalletRecord(tx *ent.Tx, record *ent.HzBlockchainWalletRecord) error {
	clientDao := l.svcCtx.Orm.HzBlockchainWalletRecord
	if tx != nil {
		clientDao = tx.HzBlockchainWalletRecord
	}
	err := clientDao.UpdateOne(record).
		SetUpdateAt(time.Now().Unix()).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil

}
